package com.example.swimmingpool.service.course;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.swimmingpool.entity.courese_schedule.CourseSchedule;
import com.example.swimmingpool.entity.course.Course;
import com.example.swimmingpool.entity.teach.Teach;
import com.example.swimmingpool.mapper.courese_schedule.CourseScheduleMapper;
import com.example.swimmingpool.mapper.course.CourseMapper;
import com.example.swimmingpool.mapper.teach.TeachMapper;
import com.example.swimmingpool.qo.course.CourseQo;
import com.example.swimmingpool.vo.course.CourseQueryVo;
import com.example.swimmingpool.vo.course.CourseVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CourseService {
    @Resource
    private CourseMapper courseMapper;


    @Resource
    private CourseScheduleMapper courseScheduleMapper;

    @Resource
    private TeachMapper teachMapper;

    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CourseVo vo) {
        var success = 1;


        var course = BeanUtil.copyProperties(vo, Course.class);
        success &= courseMapper.insert(course);
        var id = course.getId();

        var teach = new Teach();
        teach.setCourseId(id);
        teach.setTeacherId(vo.getTeacherId());
        success &= teachMapper.insert(teach);

        var courseTime = String.join(",", vo.getCourseTime());
        var courseSchedule = new CourseSchedule(id, courseTime);
        success &= courseScheduleMapper.insert(courseSchedule);
        return success == 1;

    }

    public IPage<CourseQueryVo> list(CourseQo qo) {
        IPage<CourseQueryVo> page = new Page<>(qo.getCurrent(), qo.getSize());
        return courseMapper.getCourses(page, qo);
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(CourseVo vo) {
        var success = 1;

        var id = vo.getId();
        success &= courseMapper.deleteById(id);

        LambdaUpdateWrapper<CourseSchedule> courseScheduleWrapper = new LambdaUpdateWrapper<>();
        courseScheduleWrapper.eq(CourseSchedule::getCourseId, id).set(CourseSchedule::getDeleteFlag, 1);
        success &= courseScheduleMapper.update(null, courseScheduleWrapper);

        LambdaUpdateWrapper<Teach> teachWrapper = new LambdaUpdateWrapper<>();
        teachWrapper.eq(Teach::getCourseId, id).set(Teach::getDeleteFlag, 1);
        success &= teachMapper.update(null, teachWrapper);


        return success == 1;

    }


    @Transactional(rollbackFor = Exception.class)
    public Boolean update(CourseVo vo) {
        var success = 1;


        var course = BeanUtil.copyProperties(vo, Course.class);
        var id = course.getId();

        success &= courseMapper.updateById(course);


        var teach = new Teach();
        teach.setCourseId(id);
        teach.setTeacherId(vo.getTeacherId());

        LambdaUpdateWrapper<Teach> teachWrapper = new LambdaUpdateWrapper<>();
        teachWrapper.eq(Teach::getCourseId, id);
        success &= teachMapper.update(teach, teachWrapper);

        LambdaUpdateWrapper<CourseSchedule> courseScheduleWrapper = new LambdaUpdateWrapper<>();
        courseScheduleWrapper.eq(CourseSchedule::getCourseId, id);

        var courseTime = String.join(",", vo.getCourseTime());
        var courseSchedule = new CourseSchedule(id, courseTime);
        success &= courseScheduleMapper.update(courseSchedule, courseScheduleWrapper);
        return success == 1;

    }
}
